//
//  SZLGetVerfyCodeViewController.m
//  Shengzhuli
//
//  Created by 卡忙科技 on 16/7/29.
//  Copyright © 2016年 kamangkeji. All rights reserved.
//

#import "SZLGetVerfyCodeViewController.h"
#import "LoginRegisterService.h"
#import "VefityTool.h"
#import "MBProgressHUD+MJ.h"
#import "SZLRegisterViewController.h"
#import "SZLWebViewController.h"

@interface SZLGetVerfyCodeViewController ()

@property (weak, nonatomic) IBOutlet UITextField *tellField;
@property (weak, nonatomic) IBOutlet UIButton *agreeBtn;
@property (weak, nonatomic) IBOutlet UIButton *protocolBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topCons;

@property (nonatomic,assign) CodeType codeType;

@end

@implementation SZLGetVerfyCodeViewController

- (instancetype)initWithCodeType:(CodeType)type
{
    if (self = [super init]) {
        self.codeType = type;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.codeType == CodeTypeRegister) {
        self.navigationItem.title = @"新用户注册";
    }else{
        self.navigationItem.title = @"找回密码";
        self.agreeBtn.hidden = YES;
        self.protocolBtn.hidden = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
    [super viewWillAppear:animated];
}
- (IBAction)nextClick:(UIButton *)btn
{
    switch (btn.tag) {
        case 0:
        {
            [self nextStep];
        }
            break;
        case 1:
        {
            btn.selected = !btn.selected;
        }
            break;
        case 2:
        {
            [self agreeProtocolClick];
        }
            break;
            break;
        default:
            break;
    }
    
}
- (void)agreeProtocolClick
{
    SZLWebViewController *webVc = [[SZLWebViewController alloc]init];
    webVc.navigationItem.title = @"圣朱利合作协议";
    webVc.requestStr = [NSString stringWithFormat:@"%@/agreement/register",PublicServer];
    [self.navigationController pushViewController:webVc animated:YES];
}
- (void)nextStep
{
    if (!self.agreeBtn.selected&&self.codeType == CodeTypeRegister) {
        [MBProgressHUD showError:@"请同意圣朱利合作协议" toView:self.view];
        return;
    }
    if (self.tellField.text.length<1) {
        [MBProgressHUD showError:@"请输入手机号码" toView:self.view];
        return;
    }
    if (![VefityTool isValidTellNumber:self.tellField.text]) {
        [MBProgressHUD showError:@"请输入正确的手机号码" toView:self.view];
        return;
    }
    [self showWaiter];
    [LoginRegisterService getVerfyCodeWithParame:@{@"mobilePhone":self.tellField.text} response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"验证码获取成功" toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                SZLRegisterViewController *registerVc = [[SZLRegisterViewController alloc]initWithCodeType:self.codeType];
                registerVc.tell = self.tellField.text;
                [self.navigationController pushViewController:registerVc animated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}


@end
